
package phase1.part1;

import java.util.HashSet;
import java.util.Set;

/**
 * 
 * 
 * @author John Girata
 * @version Nov 17, 2012
 */
public class CharacterClass {
	private Set<Character> included;
	
	public CharacterClass() {
		included = new HashSet<>();
	}
	
	public CharacterClass(CharacterClass other) {
		included = new HashSet<>(other.included);
	}
	
	public CharacterClass(char c) {
		this();
		include(c);
	}
	
	public CharacterClass(char start, char end) {
		this();
		include(start, end);
	}
	
	public void include(char c) {
		included.add(c);
	}
	
	public void include(char start, char end) {
		while (start <= end) {
			include(start++);
		}
	}
	
	public void include(CharacterClass other) {
		included.addAll(other.included);
	}
	
	public void exclude(CharacterClass other) {
		included.removeAll(other.included);
	}
	
	public boolean contains(char c) {
		return included.contains(c);
	}

	public Set<Character> getIncluded() {
		return included;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((included == null) ? 0 : included.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (!(obj instanceof CharacterClass))
			return false;
		CharacterClass other = (CharacterClass) obj;
		if (included == null) {
			if (other.included != null)
				return false;
		} else if (!included.equals(other.included))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "CharacterClass [included=" + included + "]";
	}
}
